package com.key.oa.http;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.key.oa.activity.LoginActivity;

import android.app.AlertDialog;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HttpUtil {

	public String get(String uri) {
		String strResult = "";
		try {
			HttpClient getClient = new DefaultHttpClient();
			HttpGet request = new HttpGet(uri);
			HttpResponse response = getClient.execute(request);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				strResult = EntityUtils.toString(response.getEntity(), "UTF-8");
			} else {
				strResult = "";
			}
		} catch (Exception e) {
			e.printStackTrace();
			strResult = "";
		}
		return strResult;
	}

	public static String put(String uri, List<NameValuePair> params) {

		try {
			// 新建HttpPost对象
			HttpPost httpPost = new HttpPost(uri);
			// 设置字符集
			HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
			// 设置参数实体
			httpPost.setEntity(entity);
			// 获取HttpClient对象
			HttpClient httpClient = new DefaultHttpClient();
			// 获取HttpResponse实例
			HttpResponse httpResp = httpClient.execute(httpPost);
			// 判断是够请求成功
			if (httpResp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 获取返回的数据
				String result = EntityUtils.toString(httpResp.getEntity(),
						"UTF-8");
				return result;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
